﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevFx.FrameWork.Exception;

namespace DevFx.FrameWork.Pager
{ /// <summary>
    /// 复合查询条件
    /// </summary>
    [Serializable]
    public class ComplexSearchCondition : SearchCondition
    {
        #region 私有变量

        ConditionRelation _ConditionRelation = ConditionRelation.And;

        #endregion

        #region 构造函数



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conditionName">查询条件</param>
        /// <param name="conditionValue">条件值</param>
        public ComplexSearchCondition(string conditionName, string conditionValue)
            : base(conditionName, conditionValue)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conditionName">查询条件</param>
        /// <param name="conditionValue">条件值</param>
        /// <param name="mode">查询条件模式</param>
        public ComplexSearchCondition(string conditionName, string conditionValue, SearchMode mode)
            : base(conditionName, conditionValue, mode)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fisrtCondition">第一个查询条件</param>
        /// <param name="secondCondition">第二个查询条件</param>
        public ComplexSearchCondition(ComplexSearchCondition fisrtCondition, ComplexSearchCondition secondCondition)
            : this(fisrtCondition, secondCondition, ConditionRelation.And)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fisrtCondition">第一个查询条件</param>
        /// <param name="secondCondition">第二个查询条件</param>
        /// <param name="conditionRelation">查询条件关系</param>
        public ComplexSearchCondition(ComplexSearchCondition fisrtCondition, ComplexSearchCondition secondCondition, ConditionRelation conditionRelation)
        {
            if (fisrtCondition == null || secondCondition == null)
                throw new FrameException("查询对象ComplexSearchCondition构造时不能传递空参数");

            this.FisrtCondition = fisrtCondition;
            this.SecondCondition = secondCondition;
            this.ConditionRelation = conditionRelation;
        }

        #endregion

        #region 公共属性



        /// <summary>
        /// 第一个子条件
        /// </summary>
        public ComplexSearchCondition FisrtCondition
        {
            get;
            protected set;
        }

        /// <summary>
        /// 第二个子条件
        /// </summary>
        public ComplexSearchCondition SecondCondition
        {
            get;
            protected set;
        }

        /// <summary>
        /// 2个条件之间的关系
        /// </summary>
        public ConditionRelation ConditionRelation
        {
            get
            {
                return _ConditionRelation;
            }
            protected set
            {
                _ConditionRelation = value;
            }
        }

        /// <summary>
        /// 是否叶子查询条件
        /// </summary>
        public bool IsLeafageCondition
        {
            get
            {
                return FisrtCondition == null && SecondCondition == null;
            }
        }


        #endregion

        /// <summary>
        /// 检索条件的Sql字符串形式
        /// </summary>
        public string ToSqlString()
        {
            return " and" + GetSqlCondition(this);
        }

        #region 私有方法

        /// <summary>
        /// 获取检索条件的sql字符串形式
        /// </summary>
        /// <param name="cond">检索条件</param>
        /// <returns></returns>
        private string GetSqlCondition(ComplexSearchCondition cond)
        {
            if (cond.IsLeafageCondition)
            {
                if (cond.ConditionValue == "")
                    return " 1=1";

                switch (cond.SearchMode)
                {
                    case SearchMode.Contain:
                        return " " + cond.ConditionName + " like '%" + cond.ConditionValue + "%'";
                    case SearchMode.ContainRight:
                        return " " + cond.ConditionName + " like '" + cond.ConditionValue + "%'";
                    case SearchMode.In:
                        return " " + cond.ConditionName + " in (" + cond.ConditionValue + ")";
                    case SearchMode.Equal:
                        return " " + cond.ConditionName + " = '" + cond.ConditionValue + "'";
                    case SearchMode.LessThan:
                        return " " + cond.ConditionName + " < '" + cond.ConditionValue + "'";
                    case SearchMode.LessThanEqual:
                        return " " + cond.ConditionName + " <='" + cond.ConditionValue + "'";
                    case SearchMode.MoreThan:
                        return " " + cond.ConditionName + " >'" + cond.ConditionValue + "'";
                    case SearchMode.MoreThanEqual:
                        return " " + cond.ConditionName + " >='" + cond.ConditionValue + "'";
                    case SearchMode.TimeNot:
                        return " datepart(year," + cond.ConditionName + ") <>'" + cond.ConditionValue + "'";
                    default:
                        return " " + cond.ConditionName + " like '%" + cond.ConditionValue + "%'";
                }
            }
            else
            {
                switch (cond.ConditionRelation)
                {
                    case ConditionRelation.And:
                        return " (" + GetSqlCondition(cond.FisrtCondition) + " and " + GetSqlCondition(cond.SecondCondition) + ")";
                    case ConditionRelation.Or:
                        return " (" + GetSqlCondition(cond.FisrtCondition) + " or " + GetSqlCondition(cond.SecondCondition) + ")";
                    default:
                        return " (" + GetSqlCondition(cond.FisrtCondition) + " or " + GetSqlCondition(cond.SecondCondition) + ")";
                }
            }
        }

        #endregion
    }
}
